
/**  
* @Title: MetaDataTemplate.java  
* @Package org.sapling.data.beans  
* @Description: TODO
* @author DcfZero  
* @date 2021年5月12日  
* @version V1.0  
*/ 
package org.sapling.data.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import lombok.Getter;

/**  
* TODO
* @ClassName: MetaDataTemplate  
* @author DcfZero 
* @date 2021年5月12日  
*    
*/
@Getter
public class MetadataTemplate implements Serializable{

	/**
	 * TODO
	 * @author DcfZero
	 */
	private static final long serialVersionUID = 848399075877465573L;
	//private static final String queryString = "SELECT * FROM sys_table_metadata ORDER BY TABLE_ID,FIELD_SEQ";
	private static final String queryString = "SELECT sys_table_metadata.*,sys_config_connection.i_datasource_type FROM sys_table_metadata,sys_table,sys_config_connection WHERE sys_table_metadata.table_id = sys_table.table_id and sys_table.connect_id = sys_config_connection.c_datasource_connectId ORDER BY TABLE_ID,FIELD_SEQ";
	private String pkUid;
	private String tableId;
	private String tableName;
	private Integer dataSourceType;
	private List<MetadataFieldTemplate> fieldList = new ArrayList<MetadataFieldTemplate>();
	
	public MetadataTemplate(Map<String,Object> template) {
		if(!template.isEmpty()) {
			this.setPkUid(String.valueOf(template.get("pk_uid")));
			this.setTableId(String.valueOf(template.get("table_id")));
			this.setTableName(String.valueOf(template.get("table_name")));
			this.setField(template);
			this.setDataSourceType(Integer.valueOf(Integer.parseInt(template.get("i_datasource_type") + "")));
		}
	}
	
	public static String getQueryString() {
		return queryString;
	}

	private void setPkUid(String pkUid) {
		this.pkUid = pkUid;
	}

	private void setTableId(String tableId) {
		this.tableId = tableId;
	}

	private void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public MetadataFieldTemplate setField(Map<String,Object> template) {
		MetadataFieldTemplate metadataFieldTemplate = new MetadataFieldTemplate(template);
		this.fieldList.add(metadataFieldTemplate);
		return metadataFieldTemplate;
	}
	
	public String getPkField() {
		for(MetadataFieldTemplate field : fieldList) {
			if(!StringUtils.isBlank(field.getFieldIsKey()) && field.getFieldIsKey().toUpperCase().equals("Y")) {
				return field.getTableField();
			}
		}
		return null;
	}

	private void setDataSourceType(Integer dataSourceType) {
		this.dataSourceType = dataSourceType;
	}
	
	
}
